<script lang="ts" setup>
import { ref } from "vue"
import { ElMessage, ElMessageBox } from "element-plus"
import { CirclePlus } from "@element-plus/icons-vue"
import { deleteMenuDataApi, getMenuListApi } from "@/api/system"
import { type DeleteRequestData, type MenusData } from "@/api/system/types/menu"
import doEditDialog from "./components/doEdit.vue"

defineOptions({
  // 命名当前组件
  name: "menuIndexPage"
})
const loading = ref<boolean>(false)
const doEdits = ref<any>()

//#region 查
const tableData = ref<MenusData[]>([])
const getTableData = async () => {
  loading.value = true
  const { data } = await getMenuListApi({})
  tableData.value = data
  loading.value = false
}
const DialogClose = async () => {
  await getTableData()
}
getTableData()
//#endregion

//#region 改
const currentUpdateId = ref<undefined | string>(undefined)
const handleUpdate = (row: any) => {
  if (row == undefined) {
    doEdits.value.handleUpdate()
  } else {
    currentUpdateId.value = row.id
    doEdits.value.handleUpdate(row)
  }
}

//#endregion

//#region 删
const handleDelete = (row: DeleteRequestData) => {
  ElMessageBox.confirm(`正在删除菜单：${row.title}，确认删除？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    deleteMenuDataApi({ id: row.id }).then(() => {
      ElMessage.success("删除成功")
      getTableData()
    })
  })
}
//#endregion
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-button type="primary" :icon="CirclePlus" @click="handleUpdate">新增</el-button>
          <!--          <el-button type="danger" :icon="Delete">批量删除</el-button>-->
        </div>
        <div>
          <!--          <el-tooltip content="下载">-->
          <!--            <el-button type="primary" :icon="Download" circle />-->
          <!--          </el-tooltip>-->
          <el-tooltip content="刷新当前页">
            <!--            <el-button type="primary" :icon="RefreshRight" circle @click="getTableData" />-->
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData" row-key="id" border>
          <el-table-column prop="id" label="编号" sortable />
          <el-table-column prop="title" label="菜单名称" />
          <el-table-column prop="path" label="路由地址" />
          <el-table-column prop="name" label="路由名称" />
          <el-table-column prop="component" label="映射组件名称" />
          <!--          <el-table-column prop="redirect" label="重定向路由" />-->
          <el-table-column prop="hidden" label="菜单隐藏路由">
            <template #default="scope">
              <el-tag v-if="scope.row.hidden == 1" type="success" effect="plain">接口</el-tag>
              <el-tag v-else type="danger" effect="plain">菜单</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="icon" label="svg图标" />
          <el-table-column prop="parent_Name" label="上级路由" />
          <el-table-column prop="controller" label="控制器" />
          <el-table-column prop="http_name" label="模块名" />
          <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="scope">
              <template v-if="scope.row.id != 1 && scope.row.id != 2">
                <el-button type="primary" text bg size="small" @click="handleUpdate(scope.row)">修改</el-button>
                <el-button type="danger" text bg size="small" @click="handleDelete(scope.row)">删除</el-button>
              </template>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <doEditDialog ref="doEdits" @dialog-close="DialogClose" />
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;

  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
